﻿package net.sealuitk.containers 
{
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import net.sealuitk.Container;
	import net.sealuitk.UIConst;
	/**
	 * ...
	 * @author mkn
	 */
	public class Row extends Container {

		public function Row(Name:String, Width:Number, Height:Number, AlignHorizontal:String = UIConst.ALIGN_EVEN, AlignVertical:String = UIConst.ALIGN_CENTER) {
			setAlignments(AlignHorizontal, AlignVertical);
			super(Name, Width, Height);
		}
		
		override public function updateFixed():void {
			super.updateFixed();
			
			var flowX:Number = 0;
			var flowY:Number = 0;
			var maxWidth:Number = 0;
			var maxHeight:Number = 0;
			var gadgetWidth:Number = 0;
			var gadgetHeight:Number = 0;
			
			var i:int = 0
			
			for (i = 0; i < contentFixed.length ; i++) {
				maxWidth = Math.max(contentFixed[i].width, maxWidth);
				maxHeight = Math.max(contentFixed[i].height, maxHeight);
				gadgetWidth += contentFixed[i].width;
				gadgetHeight += contentFixed[i].height;
			}
			
			if (sizeSet == false) {
				size.x = padding.left + padding.right + gadgetWidth + (contentFixed.length + 1) * contentPadding.x;
				size.y = padding.top + padding.bottom + maxHeight;
			}
			
			switch(alignHorizontal) {
				case UIConst.ALIGN_LEFT:
					flowX = padding.left;
					flowX += contentPadding.x;
					for (i = 0; i < contentFixed.length ; i++) {
						contentFixed[i].x = flowX;
						flowX += contentFixed[i].width + contentPadding.x;
					}
				break;
				case UIConst.ALIGN_MIDDLE:
					flowX = ( width - (padding.left + padding.right) ) / 2 - ( gadgetWidth + ( (contentFixed.length + 1 ) * contentPadding.x) ) / 2;
					flowX += padding.left;
					flowX += contentPadding.x;
					
					for (i = 0; i < contentFixed.length ; i++) {
						contentFixed[i].x = flowX;
						flowX += contentFixed[i].width + contentPadding.x;
					}
				break; 
				case UIConst.ALIGN_RIGHT:
					flowX = width - padding.right;
					for (i = contentFixed.length ; --i > -1 ; ) {
						flowX -= ( contentFixed[i].width + contentPadding.x );
						contentFixed[i].x = flowX;
					}
				break; 
				case UIConst.ALIGN_EVEN:
					var between:Number = (width - gadgetWidth - (padding.left + padding.right)) / (contentFixed.length + 1);
					flowX = padding.left + between;
					for (i = 0; i < contentFixed.length ; i++) {
						contentFixed[i].x = flowX;
						flowX += contentFixed[i].width + between;
					}
				break;
				case UIConst.ALIGN_MAGIC:
					// TODO: alignHorizontal MAGIC
					throw Error("Not implemented");
				break;
				default:
					throw Error("Inappropriate horizontal alignment: " + alignHorizontal);
			}
			
			switch(alignVertical) {
				case UIConst.ALIGN_TOP:
				case UIConst.ALIGN_EVEN:
				case UIConst.ALIGN_MAGIC:
					for (i = 0; i < contentFixed.length ; i++) {
						contentFixed[i].y = padding.top + contentPadding.y;
					}
				break;
				case UIConst.ALIGN_CENTER:
					for (i = 0; i < contentFixed.length ; i++) {
						contentFixed[i].y = (height - contentFixed[i].height) / 2;// + contentFixed[i] / 2;
						contentFixed[i].y += padding.top + contentPadding.y;
					}
				break;
				case UIConst.ALIGN_BOTTOM:
					for (i = 0; i < contentFixed.length ; i++) {
						contentFixed[i].y = height - contentFixed[i].height - padding.bottom - contentPadding.y;
					}
				break;
				default:
					throw Error("Inappriate vertical alignment: " + alignVertical);
			}
			
			if (sizeSet == false) {
				updateBackground();
				updateFloat();
				updateFrames();
			}
		}
	}
	
}